package com.spc.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.spc.constant.MessageConstant;
import com.spc.constant.RedisMessageConstant;
import com.spc.entity.Result;
import com.spc.pojo.Order;
import com.spc.service.MapService;
import com.spc.service.OrderService;
import com.spc.utils.SMSUtils;
import com.spc.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;

/**
 *在线体检预约处理
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private JedisPool jedisPool;
    @Reference
    private OrderService orderService;
    @Reference
    private MapService mapService;


    //用户预约发送验证码
    @RequestMapping("/submit")
    public Result submit (@RequestBody Map map){
        //从redis中取出验证码
        String telephone = (String) map.get("telephone");
        String validateCodeInRedis = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        //判断验证码是否正确
        String validateCode = (String) map.get("validateCode");
        if (validateCodeInRedis != null && validateCode!= null && validateCode.equals(validateCodeInRedis)){
            //成功，完成预约业务处理
            map.put("orderType", Order.ORDERTYPE_WEIXIN);//设置预约类型
            Result result = null;
            try {
                result = orderService.order(map);
            } catch (Exception e) {
                e.printStackTrace();
                return result;
            }
            if (result.isFlag()){
                //随机生成验证码
                Integer validateCode2 = ValidateCodeUtils.generateValidateCode(4);
                //预约成功,发送短信
                try {
                    SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,telephone, validateCode2.toString());
                } catch (ClientException e) {
                    e.printStackTrace();
                }

            }
            return result;
        }else {
            //不成功，返回结果
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
    }
    @RequestMapping("/findById")
    public Result findById (Integer id){
        try {
            Map map = orderService.findById(id);
            return new Result(true,MessageConstant.QUERY_SETMEALLIST_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }

    @RequestMapping("/findAddress")
    public Result findAddress(){
        try {
            List<String> list = mapService.findAddress();
            return new Result(true,"",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }


}
